package com.example.smartcity_poverty.ui.Home.Seek

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Html
import android.view.MenuItem
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.util.Util
import com.example.smartcity_poverty.ui.ShowNewsActivity
import com.example.smartcity_poverty.ui.Home.HomeViewModel
import kotlinx.android.synthetic.main.activity_seek.*
import kotlinx.android.synthetic.main.re_item_news.*

/**
 * 搜索实现
 */
class SeekActivity : AppCompatActivity() {

    private val viewMode by lazy {
        ViewModelProviders.of(this)[HomeViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_seek)
        title = "搜索结果"
        val content = intent.getStringExtra("content").toString()
        seek(content)
    }

    // 模糊查询
    private fun seek(content: String){
        val array = ArrayList<News>()
        // 适配器
        SeekRecycler.apply {
            this.layoutManager = LinearLayoutManager(context)
            this.adapter = Adapter(array, R.layout.re_item_news)
        }

        // 获取数据
        viewMode.setNewsType(null)
        viewMode.newsLiveData.observe(this, Observer { resources ->
            val body = resources.getOrNull()
            if (body != null){
                viewMode.newsList.clear()
                viewMode.newsList.addAll(body.rows!!)
            }

            var aa = 0
            for (i in viewMode.newsList){
                i.title?.forEach { ti ->
                    if (aa == 1) return@forEach
                    content.forEach { con ->
                        if (ti == con){
                            array.add(News(url + i.cover, i.title.toString(), i.content.toString(), i.publishDate.toString(), i.likeNum.toString(), i.id.toString()))
                            SeekRecycler.adapter?.notifyItemChanged(array.size)
                            aa = 1
                        }
                    }
                }
                aa = 0
            }
        })
    }


    // Model
    inner class News(val image: String, val title: String, val content: String, val time: String, val userSize: String, val id: String)

    @Suppress("DEPRECATION")
    inner class Adapter(
        private val list: MutableList<News>,
        private val layoutId: Int
    ) : BaseRecyclerAdapter<News>(list, layoutId){
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: News,
            position: Int,
            holder: BaseViewHolder
        ) {
            if (list.size <= 0){

            }else{
                val d = list[position]
                Util.glide(d.image, holder.viewFind.newsImage)
                holder.viewFind.newsTitle.text = d.title
                holder.viewFind.newsContent.text = Html.fromHtml(d.content)
                holder.viewFind.newsTime.text = d.time
                val si = d.userSize.toString() + "评论"
                holder.viewFind.newsUserSize.text = si

                val con = holder.viewFind.view?.context
                holder.itemView.setOnClickListener {
                    con?.startActivity(Intent(con, ShowNewsActivity::class.java).apply {
                        putExtra("id", d.id)
                    })
                }
            }

        }

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}
